package com.chookss.login

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.chookss.MyApp
import com.chookss.R
import com.chookss.tiku.FeedbackActivity
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.johnrambo.ktea.common.logger
import com.johnrambo.ktea.ktExtends.onclick
import com.johnrambo.ktea.ktExtends.startActivity
import com.lvgroup.hospital.base.BaseAct
import kotlinx.android.synthetic.main.activity_guide.*

class GuideActivity : BaseAct() {

    //容器
    private val mList = mutableListOf<View>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_guide)
    }


    override fun initData() {
        //设置默认图片
        initState2()
        setPointImg(true, false, false)

        //第三页的按钮点击事件
//        btn_start onclick {
//            //startActivity(Intent(this, MainActivity::class.java))
//            startActivity<LoginActivity>()
//            finish()
//        }
        btn_start.setOnClickListener {
            var intent = Intent(this, LoginActivity::class.java)
            intent.putExtra("isFirst", true)
            startActivity(intent)
            finish()
        }

        //跳过按钮
        iv_back onclick {
            startActivity<LoginActivity>()
            finish()
        }

        val view1 = View.inflate(this,
            R.layout.pager_item_one, null)
        val view2 = View.inflate(this,
            R.layout.pager_item_two, null)
        val view3 = View.inflate(this,
            R.layout.pager_item_three, null)
        mList.add(view1)
        mList.add(view2)
        mList.add(view3)
        //设置适配器
        mViewPager.adapter = GuideAdapter()
        //监听ViewPager的滑动
        mViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int) {
            }
            //切换的回调
            override fun onPageSelected(position: Int) {
                logger("position" + position)
                when (position) {
                    0 -> {
                        setPointImg(true, false, false)
                        btn_start.visibility = View.GONE
                        iv_back.visibility = View.VISIBLE
                        mLinearLayout.visibility = View.VISIBLE
                    }
                    1 -> {
                        setPointImg(false, true, false)
                        btn_start.visibility = View.GONE
                        iv_back.visibility = View.VISIBLE
                        mLinearLayout.visibility = View.VISIBLE
                    }
                    2 -> {
                        setPointImg(false, false, true)
                        btn_start.visibility = View.VISIBLE
                        iv_back.visibility = View.GONE
                        mLinearLayout.visibility = View.VISIBLE
                        getPermissions()
                    }
                }
            }

        })
    }
    //获取存储权限
    private fun getPermissions() {
        XXPermissions.with(this)
            .permission(Permission.Group.STORAGE)
            .request(object : OnPermissionCallback {
                override fun onGranted(granted: List<String>, all: Boolean) {

                }

                override fun onDenied(denied: List<String>, quick: Boolean) {

                }
            })
    }
    override fun onBackPressed() {
        startActivity<LoginActivity>()
        finish()
    }

    //设置小圆点的选中效果
    fun setPointImg(isCheck1: Boolean, isCheck2: Boolean, isCheck3: Boolean) {
        if (isCheck1) {
            point1.setBackgroundResource(R.drawable.shape_circle_main_color)
        } else {
            point1.setBackgroundResource(R.drawable.shape_circle_c2c2c2)
        }

        if (isCheck2) {
            point2.setBackgroundResource(R.drawable.shape_circle_main_color)
        } else {
            point2.setBackgroundResource(R.drawable.shape_circle_c2c2c2)
        }

        if (isCheck3) {
            point3.setBackgroundResource(R.drawable.shape_circle_main_color)
        } else {
            point3.setBackgroundResource(R.drawable.shape_circle_c2c2c2)
        }

    }

    //适配器
    inner class GuideAdapter() : PagerAdapter() {

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view == `object`
        }

        override fun getCount(): Int {
            return mList.size
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            (container as ViewPager).addView(mList.get(position))
            return mList.get(position)
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            (container as ViewPager).removeView(mList.get(position))
            // super.destroyItem(container, position, `object`)
        }

    }
}